package com.zhou.plugin.packagejars.ui;

import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.compiler.CompilerManager;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiPackage;
import com.intellij.util.Consumer;
import com.zhou.plugin.packagejars.pack.impl.AllPacker;
import com.zhou.plugin.packagejars.pack.impl.EachPacker;
import com.zhou.plugin.packagejars.util.Util;

import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Setting extends JDialog {
	private static File tempFile = new File(System.getProperty("java.io.tmpdir") + "idea-plugin-package-path.properties");
	private DataContext dataContext;

	private JPanel contentPane;
	private JButton buttonOK;
	private JButton buttonCancel;
	private JTextField exportDirectoryField;
	private JButton selectPathButton;
	private JTextField exportJarNameField;
	private JCheckBox namedByPackageCheckBox;
	private JCheckBox exportEachChildrenCheckBox;
	private JCheckBox fastModeCheckBox;

	public Setting(DataContext dataContext) {
		this.dataContext = dataContext;

		setContentPane(contentPane);
		setModal(true);
		getRootPane().setDefaultButton(buttonOK);

		buttonOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onOK();
			}
		});

		buttonCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onCancel();
			}
		});
		setDefaultCloseOperation(0);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				onCancel();
			}
		});

		contentPane.registerKeyboardAction(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onCancel();
			}
		}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

		namedByPackageCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onNamedByPackageCheckBoxChange();
			}
		});
		selectPathButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				onSelectPathButtonAction();
			}
		});
		Project project = (Project) CommonDataKeys.PROJECT.getData(this.dataContext);
		Module module = (Module) LangDataKeys.MODULE.getData(this.dataContext);
		VirtualFile[] virtualFiles = (VirtualFile[]) CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(this.dataContext);

		List<String> names = new ArrayList();
		for (VirtualFile file : virtualFiles) {
			PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(file);
			if (psiDirectory != null) {
				PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
				names.add(psiPackage.getQualifiedName());
			}
		}
		String jarName = Util.getTheSameStart(names);
		if (jarName.equals("")) {
			jarName = module.getName();
		}
		if (jarName.endsWith(".")) {
			jarName = jarName.substring(0, jarName.lastIndexOf("."));
		}
		this.exportJarNameField.setText(jarName);

		System.out.println(tempFile.getPath());
		try {
			if (!tempFile.exists()) {
				tempFile.createNewFile();
			}
			Properties properties = new Properties();
			InputStream in = new FileInputStream(tempFile);
			properties.load(in);
			Object exportPath = properties.get("export_path");
			if (exportPath != null) {
				this.exportDirectoryField.setText(exportPath.toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Setting dialog = new Setting(null);
		dialog.pack();
		dialog.setVisible(true);
		System.exit(0);
	}

	private void onNamedByPackageCheckBoxChange() {
		if (this.namedByPackageCheckBox.isSelected()) {
			this.exportJarNameField.setEnabled(false);
		} else {
			this.exportJarNameField.setEnabled(true);
		}

	}

	private void onSelectPathButtonAction() {
		Project project = (Project) CommonDataKeys.PROJECT.getData(this.dataContext);
		FileChooserDescriptor descriptor = FileChooserDescriptorFactory.createSingleFolderDescriptor();
		Setting.FileChooserConsumerImpl chooserConsumer = new Setting.FileChooserConsumerImpl(this.exportDirectoryField);
		FileChooser.chooseFile(descriptor, project, (VirtualFile) null, chooserConsumer);
	}

	private void onCancel() {
		this.dispose();
	}

	private void onOK() {
		Project project = (Project) CommonDataKeys.PROJECT.getData(this.dataContext);
		Module module = (Module) LangDataKeys.MODULE.getData(this.dataContext);
		String exportJarName = this.exportJarNameField.getText();
		exportJarName = exportJarName.trim();
		exportJarName = exportJarName + ".jar";
		if (Util.matchFileNamingConventions(exportJarName) && (!exportJarName.equals("") || this.namedByPackageCheckBox.isSelected())) {
			String exportJarPath = this.exportDirectoryField.getText().trim();
			File _temp0 = new File(exportJarPath);
			if (!_temp0.exists()) {
				Messages.showErrorDialog(project, "the selected output path is not exists", "");
			} else {
				if (this.exportEachChildrenCheckBox.isSelected()) {
					EachPacker packager = new EachPacker(this.dataContext, exportJarPath);
					if (this.fastModeCheckBox.isSelected()) {
						CompilerManager.getInstance(project).make(module, packager);
					} else {
						CompilerManager.getInstance(project).compile(module, packager);
					}
				} else {
					AllPacker packager1 = new AllPacker(this.dataContext, exportJarPath, exportJarName);
					if (this.fastModeCheckBox.isSelected()) {
						CompilerManager.getInstance(project).make(module, packager1);
					} else {
						CompilerManager.getInstance(project).compile(module, packager1);
					}
				}

				this.dispose();
			}
		} else {
			Messages.showErrorDialog(project, "please set a name of the output jar", "");
		}
	}

	private class FileChooserConsumerImpl implements Consumer<VirtualFile> {
		private JTextField ouPutDirectoryField;

		public FileChooserConsumerImpl(JTextField jTextField) {
			this.ouPutDirectoryField = jTextField;
		}

		public void consume(VirtualFile virtualFile) {
			this.ouPutDirectoryField.setText(virtualFile.getPath());

			try {
				if (!Setting.tempFile.exists()) {
					Setting.tempFile.createNewFile();
				}

				Properties e = new Properties();
				FileOutputStream out = new FileOutputStream(Setting.tempFile);
				e.setProperty("export_path", virtualFile.getPath());
				e.store(out, "The New properties file");
				out.close();
			} catch (IOException var4) {
				var4.printStackTrace();
			}

		}
	}
}
